package cn.sdhuijin.healthexam.control;

import java.util.Properties;

import android.app.Activity;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import cn.sdhuijin.healthexam.util.Constants;

public class RadioCheckUnionViewControl implements IControl,
		OnCheckedChangeListener {

	private Activity activity;
	private int[] checkBoxId;
	private String[] names;
	private RadioGroup radioGroup;
	private String enableStr;

	public RadioCheckUnionViewControl(Activity activity, int[] checkBoxId,
			String[] names, int radioGroupId, String enableStr) {
		this.activity = activity;
		this.checkBoxId = checkBoxId;
		this.names = names;
		this.radioGroup = (RadioGroup) activity.findViewById(radioGroupId);
		radioGroup.setOnCheckedChangeListener(this);
		this.enableStr = enableStr;
	}

	@Override
	public void getValue(Properties prop) {
		try {
			int selectId = radioGroup.getCheckedRadioButtonId();
			RadioButton radioButton = (RadioButton) activity
					.findViewById(selectId);
			if (radioButton != null) {
				String value = radioButton.getText().toString();
				prop.setProperty(names[0], value);
				if (enableStr.equals(value)) {
					StringBuffer buffer = new StringBuffer();
					for (int i = 0; i < checkBoxId.length; i++) {
						CheckBox checkbox = (CheckBox) activity
								.findViewById(checkBoxId[i]);
						if (checkbox.isChecked()) {
							buffer.append(checkbox.getText().toString());
							buffer.append(Constants.SPLIT_TAG);
						}
					}
					if (buffer != null
							&& !Constants.STR_NULL.equals(buffer.toString())) {
						prop.setProperty(names[1], buffer.toString());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setValue(Properties prop) {
		try {
			String value = prop.getProperty(names[0]);
			if (value != null) {
				int num = this.radioGroup.getChildCount();
				for (int i = 0; i < num; i++) {
					RadioButton radioButton = (RadioButton) this.radioGroup
							.getChildAt(i);
					if (value.equals(radioButton.getText().toString())) {
						this.radioGroup.check(radioButton.getId());
						break;
					}
				}
				if (value.equals(enableStr)) {
					String checkVal = prop.getProperty(names[1]);
					if (checkVal != null) {
						String[] str = checkVal.split(Constants.SPLIT_TAG);
						for (int i = 0; i < checkBoxId.length; i++) {
							for (int j = 0; j < str.length; j++) {
								CheckBox checkBox = (CheckBox) activity
										.findViewById(checkBoxId[i]);
								if (str[j]
										.equals(checkBox.getText().toString())) {
									checkBox.setChecked(true);
								}
							}
						}

					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		int selectId = group.getCheckedRadioButtonId();
		RadioButton radioButton = (RadioButton) activity.findViewById(selectId);
		if (radioButton != null) {
			String str = radioButton.getText().toString();
			if (Constants.RAD_NO.equals(str)) {
				setCheckBoxEnable(false);
			} else if (Constants.RAD_YES.equals(str)) {
				setCheckBoxEnable(true);
			}
		}
	}

	private void setCheckBoxEnable(boolean bool) {
		try {
			for (int i = 0; i < checkBoxId.length; i++) {
				CheckBox checkBox = (CheckBox) activity
						.findViewById(checkBoxId[i]);
				if (!bool) {
					checkBox.setChecked(bool);
				}
				checkBox.setEnabled(bool);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
